/**
 * FileName: AirShowIndexService
 * Author:   Leopold
 * Date:     2018/9/11 16:33
 * Description:
 * History:
 */
package cn.mcsys.viewer.core.service;

import cn.mcsys.myadmin.common.config.LoadParkingLotConfiguration;
import cn.mcsys.myadmin.common.utils.HttpRequestUtil;
import cn.mcsys.myadmin.common.utils.MapUtils;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.viewer.entity.AirParkingLocation;
import cn.mcsys.myadmin.modules.viewer.entity.AirViewerUser;
import cn.mcsys.viewer.core.utils.HttpRequestLocal;
import cn.mcsys.viewer.core.web.vo.AirLocationVO;
import cn.mcsys.viewer.core.web.vo.AirSelectPointVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.Point2D;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AirShowIndexService {

    private static Set<Map.Entry<String, List<Point2D.Double>>> entries = LoadParkingLotConfiguration.parkingLotMap.entrySet();

    @Autowired
    private BackService backService;

    public String airshowIndex() throws ParseException {
        //算出是距离航展第几天还是开始第几天
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date current = simpleDateFormat.parse(simpleDateFormat.format(date));
        Date parse = simpleDateFormat.parse("2018-11-06");
        long day=(parse.getTime()-current.getTime())/(24*60*60*1000) ;
        String dayMsg = "";
        if(day <= 0){
            day = Math.abs(day);
            day += 1;
            dayMsg = "航展专业日第"+Math.abs(day)+"天";
            if(Math.abs(day) > 3){
                day -= 3;
                dayMsg = "航展公众日第"+Math.abs(day)+"天";
            }
            if (day > 3){
                dayMsg = "航展已结束";
            }
            System.out.println(dayMsg);
        }else if(day > 0){
            dayMsg = "离航展还有"+day+"天";
            System.out.println(dayMsg);
        }
        return dayMsg;
    }


    public AirSelectPointVO setNearPoint(Double lon, Double lat, List<AirRidePoint> points) throws Exception {
        AirSelectPointVO airSelectPointVO = new AirSelectPointVO();
        HashMap<String, List<AirRidePoint>> map = new HashMap<>();
        List<AirRidePoint> nowCityRidePoint = new ArrayList<>();
        Double maxDistance = 9999999999999999999999D;
//        查询经纬度的所对应的城市
        String[] cityName = MapUtils.getCityName(String.valueOf(lon), String.valueOf(lat));
        for (AirRidePoint point : points) {
            String pointLon = point.getLon();
            String pointLat = point.getLat();
            Double distance = MapUtils.getDistance(lat, lon, Double.parseDouble(pointLat), Double.parseDouble(pointLon));
            if (distance < maxDistance.doubleValue()){
                airSelectPointVO.setNearRidePoint(point);
                maxDistance = distance;
            }
            if(point.getAirParkingLot() != null){
                if(cityName[1].contains(point.getCity())){
                    nowCityRidePoint.add(point);
                }
            }
        }
        airSelectPointVO.setNowCityRidePoint(nowCityRidePoint);
        return airSelectPointVO;
    }

    /**
     * 判断是否在停车场中
     * @param lon
     * @param lat
     */
    public String isInParkingLot(Double lon, Double lat) {
        String parkingLotName = null;
        for (Map.Entry<String, List<Point2D.Double>> entry : entries) {
            List<Point2D.Double> parkingLot = entry.getValue();
            if(MapUtils.IsPtInPoly(new Point2D.Double(lon,lat),parkingLot)){
                parkingLotName = entry.getKey();
            }
        }
        return parkingLotName;
    }

    public AirParkingLocation saveLocation(Double lon, Double lat) throws Exception {
        AirViewerUser user = HttpRequestLocal.getUserSession();
        AirParkingLocation airParkingLocation = new AirParkingLocation();
        Date date = new Date();
        String location = searchLocation(lon, lat);
        airParkingLocation.setParkingPosition(location);
        airParkingLocation.setParkingDate(date);
        airParkingLocation.setUser(user);
        airParkingLocation.setParkingLon(lon.toString());
        airParkingLocation.setParkingLat(lat.toString());
        airParkingLocation.setCreateDate(date);
        airParkingLocation = backService.saveLocation(airParkingLocation);
        String parkingLotName = null;
        for (Map.Entry<String, List<Point2D.Double>> entry : entries) {
            List<Point2D.Double> parkingLot = entry.getValue();
            if(MapUtils.IsPtInPoly(new Point2D.Double(lon,lat),parkingLot)){
                parkingLotName = entry.getKey();
            }
        }
        AirLocationVO airLocationVO = saveLocationVO(airParkingLocation,parkingLotName);
        return airLocationVO;
    }

    private AirLocationVO saveLocationVO(AirParkingLocation airParkingLocation,String parkingLotName) {
        AirLocationVO airLocationVO = new AirLocationVO();
        airLocationVO.setParkingLotName(parkingLotName);
        BeanUtils.copyProperties(airParkingLocation,airLocationVO);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(airParkingLocation.getParkingDate());
        String[] split = format.split(" ");
        airLocationVO.setParkingDay(split[0]);
        airLocationVO.setParkingTime(split[1]);
        return airLocationVO;
    }

    private String searchLocation(Double lon, Double lat) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key","d227e19b929ec1d67ce90688d38d9dea");
        map.put("location",lon+","+lat);
        String url = "https://restapi.amap.com/v3/geocode/regeo";
        String s = HttpRequestUtil.httpGet(url,map);
        JSONObject jsonObject = JSON.parseObject(s);
        JSONObject regeocode = jsonObject.getJSONObject("regeocode");
        String formattedAddress = regeocode.getString("formatted_address");
        return formattedAddress;
    }

}
